# -*- coding:utf-8 -*-
import sys
import  os
def Main_split():
    tableHead=sys.argv[2]+"\n"
    print(tableHead)
    #传参 "/tmp/中心滞留_2022-12-19.csv"
    source = sys.argv[1]
    print("目标路径是：",source)
    sourceSplit=os.path.split(sys.argv[1])
    print("目标路径切割是：",sourceSplit)
    #('/tmp', '中心滞留_2022-12-19.csv')

    dirNameArr=sourceSplit[-1].split('.')
    #['中心滞留_2022-12-19', 'csv']

    # 此处一定要建上target文件夹，不然会报路径错误
    target = sourceSplit[0]+"/"+dirNameArr[0]+"/"
    # target = 'C:\\Users\\macheng\\Desktop\\上线\\退件及时率\\'
    print("输出路径是：",target)
    # 创建文件目录
    os.makedirs(target,exist_ok=True)
    # 文件的行数的计数器
    num = 0
    # 文件序号
    name_num = 0
    # 用于存放数据
    dataStore = []
    #dataStore.append(ss)
    # 设置为UTF-8编码
    with open(source, 'r') as file_content:
        ListOfLine = file_content.read().splitlines()  # 将读取的文件内容按行分割，然后存到一个列表中
        n = len(ListOfLine)
        print("总文件行数是：",n)

    with open(source, 'r') as file_content:
        for line in file_content:
            if len(dataStore)==0:
                dataStore.append(tableHead)
            num += 1
            dataStore.append(line)
            # 设定每个文件为20万行
            if num == 100*10000-1:
                print("正在生成" , name_num , "文件")
                with open(target +dirNameArr[0]+"-"+str(name_num)+"."+dirNameArr[-1], 'w+') as file_target:
                    for data in dataStore:
                        file_target.write(data)
                print(name_num,"文件行数",len(dataStore))
                name_num += 1
                num = 0
                dataStore = []



    # 处理最后一个文件，如果最后一个文件行数少于20万行，进行如下处理
    with open(target +dirNameArr[0]+"-"+str(name_num)+"."+dirNameArr[-1], 'w+') as file_target:
        print("正在生成" , name_num , "文件")
        print(name_num , "文件行数" , len(dataStore))
        for data in dataStore:
            file_target.write(data)


if __name__ == "__main__":
    Main_split()
